package com.vs.meepet.utils;


import android.util.Log;

import com.vs.meepet.BuildConfig;


public class LogUtils {

    /**
     * default tag
     */
    public static final String TAG = "LogUtils";
    private static final int ASSERT = Log.ASSERT;
    private static final int DEBUG = Log.DEBUG;
    private static final int ERROR = Log.ERROR;
    private static final int INFO = Log.INFO;
    private static final int VERBOSE = Log.VERBOSE;
    private static final int WARN = Log.WARN;
    public static int LOGLEVEL = VERBOSE;

    private static final int MAX_LENGTH = 3 * 1024;//单次输出最长日志

    private LogUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 初始化LogUtils
     * application 中调用
     */
    public static void init() {
        LOGLEVEL = VERBOSE;
    }

    public static void v(String TAG, String s) {
        printLong(VERBOSE, TAG, s + generateTag());
    }

    public static void d(String TAG, String s) {
        printLong(DEBUG, TAG, s + generateTag());
    }


    public static void i(String TAG, String s) {
        printLong(INFO, TAG, s + generateTag());
    }

    public static void w(String TAG, String s) {
        printLong(ERROR, TAG, s + generateTag());
    }

    public static void e(String TAG, String s) {
        printLong(ERROR, TAG, s + generateTag());
    }

    public static void v(String s) {
        printLong(VERBOSE, TAG, s + generateTag());
    }


    public static void d(String s) {
        printLong(DEBUG, TAG, s + generateTag());
    }


    public static void i(String s) {
        printLong(INFO, TAG, s + generateTag());
    }

    public static void w(String s) {
        printLong(ERROR, TAG, s + generateTag());
    }

    public static void e(String s) {
        printLong(ERROR, TAG, s + generateTag());
    }

    private static void printLong(int level, String tag, Object obj) {
        if (!isShowLog()) {
            return;
        }
        String origin = obj == null ? "null" : obj.toString();
        int strLen = origin.length();
        if (strLen < MAX_LENGTH) {
            printLog(level, tag, origin);
        } else {
            StringBuilder builder = new StringBuilder(origin);
            int start = 0;
            int end = MAX_LENGTH;
            String msg;
            do {
                msg = builder.substring(start, end);
                printLog(level, tag, msg);
                start = end;
                end += MAX_LENGTH;
            } while (end < strLen);
            msg = builder.substring(start, strLen);
            printLog(level, tag, msg);
        }
    }

    private static void printLog(int level, String tag, String msg) {
        // msg = ">>>" + msg;
        switch (level) {
            case VERBOSE:
                Log.v(tag, msg);
                break;
            case DEBUG:
                Log.d(tag, msg);
                break;
            case INFO:
                Log.i(tag, msg);
                break;
            case WARN:
                Log.w(tag, msg);
                break;
            case ERROR:
            case ASSERT:
                Log.e(tag, msg);
                break;
            default:
                Log.w(tag, "LogTips:" + "没有预设的输出等级" + msg);
                break;
        }

    }

    /**
     * 获取输出日志的类，方法，输出行数
     *
     * @return
     */
    public static String generateTag() {
        String tag = "[%s.%s(L:%d)]";
        if (isShowLog()) {//不打印日志时，没必要走改方法，提升执行效率
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            if (stackTrace.length > 4) {
                StackTraceElement caller = Thread.currentThread().getStackTrace()[4];
                tag = String.format(tag, caller.getFileName(), caller.getMethodName(),
                        caller.getLineNumber());
            } else {
                tag = "LogUtils";
            }
        } else {
            tag = "LogUtils";
        }
        return tag;
    }

    private static Boolean isShowLog() {
        return BuildConfig.DEBUG;
    }


    /**
     * 获取输出日志的类的名称，可作为tag，方便查看
     *
     * @return
     */
    public static String curClazzName() {
        String tag = "LogUtils";
        if (isShowLog()) {//不打印日志时，没必要走改方法，提升执行效率
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            if (stackTrace.length > 4) {
                StackTraceElement caller = Thread.currentThread().getStackTrace()[4];
                tag = caller.getFileName();
            }
        }
        return tag;
    }

}
